package com.ys.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import com.itextpdf.text.Anchor;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Image;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.RectangleReadOnly;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.draw.DottedLineSeparator;
import com.itextpdf.text.pdf.draw.LineSeparator;

public class PdfUtil {
	
	public static void main(String[] args) throws Exception {
//		generateGoods();
//		generateStockIn();
		generatePeiHuo();
	}
	
	public static void generateGoods() {
		
		Rectangle rec = new RectangleReadOnly(400,240);
		
		try {
			// 1.新建document对象
			Document document = new Document(rec);// 建立一个Document对象
			document.setMargins(0, 0, 15f, 0);
			document.setPageCount(1);//设置生成pdf页数
			// 2.建立一个书写器(Writer)与document对象关联
			File file = new File("D://data/PDF商品标.pdf");
			file.createNewFile();
			PdfWriter.getInstance(document, new FileOutputStream(file));
			// 3.打开文档
			document.open();
			//4.生成PDF
			new PdfUtil().generatePDF(document);
			// 5.关闭文档
			document.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void generateStockIn() {
		
		Rectangle rec = new RectangleReadOnly(450, 500);
		
		try {
			// 1.新建document对象
			Document document = new Document(rec);// 建立一个Document对象
			document.setMargins(0, 0, 15f, 0);
			document.setPageCount(1);//设置生成pdf页数
			// 2.建立一个书写器(Writer)与document对象关联
			File file = new File("D://data/PDF采购入库.pdf");
			file.createNewFile();
			PdfWriter.getInstance(document, new FileOutputStream(file));
			// 3.打开文档
			document.open();
			//4.生成PDF
			new PdfUtil().generateStockIn(document);
			// 5.关闭文档
			document.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void generatePeiHuo() {
		
		Rectangle rec = new RectangleReadOnly(800, 500);
		
		try {
			// 1.新建document对象
			Document document = new Document(rec);// 建立一个Document对象
			document.setMargins(0, 0, 15f, 0);
			document.setPageCount(1);//设置生成pdf页数
			// 2.建立一个书写器(Writer)与document对象关联
			File file = new File("D://data/PDF配货清单.pdf");
			file.createNewFile();
			PdfWriter.getInstance(document, new FileOutputStream(file));
			// 3.打开文档
			document.open();
			//4.生成PDF
			new PdfUtil().generatePeiHuo(document);
			// 5.关闭文档
			document.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	// 定义全局的字体静态变量
	private static BaseFont bfChinese = createFontLocal("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);

	private static Font textfont10 = new Font(bfChinese, 10, Font.NORMAL);
	private static Font textfont12 = new Font(bfChinese, 12, Font.NORMAL);
	private static Font textfont14 = new Font(bfChinese, 14, Font.NORMAL);
	
	private static Font titlefont; //标题字体样式
	private static Font headfont;
	private static Font keyfont;
	private static Font textfont;
	// 最大宽度
	private static int maxWidth = 320;

	// 静态代码块
	static {
		try {
			// 不同字体（这里定义为同一种字体：包含不同字号、不同style）
			BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
			titlefont = new Font(bfChinese, 16, Font.BOLD);
			headfont = new Font(bfChinese, 20, Font.NORMAL);
			keyfont = new Font(bfChinese, 10, Font.BOLD);
			textfont = new Font(bfChinese, 15, Font.NORMAL);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/*
	 * 本地创建字体对象
	 */
	public static BaseFont createFontLocal(String name, String encoding, boolean embedded) {
		try {
			return BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
		} catch (DocumentException e) {
			e.printStackTrace();
			return null;
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	// 生成PDF文件
	public void generatePDF(Document document) throws Exception {
	// 段落
	Paragraph paragraph = new Paragraph("", titlefont);
	paragraph.setAlignment(Paragraph.ALIGN_LEFT); //设置文字居中 0靠左 1，居中 2，靠右
	paragraph.setIndentationLeft(12); //设置左缩进
	paragraph.setIndentationRight(12); //设置右缩进
	paragraph.setFirstLineIndent(24); //设置首行缩进
	paragraph.setLeading(20f); //行间距
	paragraph.setSpacingBefore(5f); //设置段落上空白
	paragraph.setSpacingAfter(10f); //设置段落下空白
//	paragraph.add("NEW");
	
	// 直线
	Paragraph p1 = new Paragraph();
	p1.add(new Chunk(new LineSeparator()));
	 // 点线
	 Paragraph p2 = new Paragraph();
	 p2.add(new Chunk(new DottedLineSeparator()));

	 // 超链接
	 Anchor anchor = new Anchor("baidu");
	 anchor.setReference("www.baidu.com");

	 // 定位
	 Anchor gotoP = new Anchor("goto");
	 gotoP.setReference("#top");

	 // 添加图片
	 Image image = Image.getInstance("D://data/barcode.png");
	 image.setAlignment(Image.ALIGN_CENTER);
	 image.scalePercent(100); //依照比例缩放
	 image.setSpacingBefore(0f);
//	 image.setTop(1f);
	 // 表格
	 
	 PdfPTable table;
//	 {
//		 table = createTable(new float[]{40, 120, 120, 120, 80, 80});
//		 table.addCell(createCell("美好的一天", headfont, Element.ALIGN_LEFT, 6, false));
//		 table.addCell(createCell("早上9:00", keyfont, Element.ALIGN_CENTER));
//		 table.addCell(createCell("中午11:00", keyfont, Element.ALIGN_CENTER));
//		 table.addCell(createCell("中午13:00", keyfont, Element.ALIGN_CENTER));
//		 table.addCell(createCell("下午15:00", keyfont, Element.ALIGN_CENTER));
//		 table.addCell(createCell("下午17:00", keyfont, Element.ALIGN_CENTER));
//		 table.addCell(createCell("晚上19:00", keyfont, Element.ALIGN_CENTER));
//		 Integer totalQuantity = 0;
//		 for (int i = 0; i < 5; i++) {
//		     table.addCell(createCell("起床", textfont));
//		     table.addCell(createCell("吃午饭", textfont));
//		     table.addCell(createCell("午休", textfont));
//		     table.addCell(createCell("下午茶", textfont));
//		     table.addCell(createCell("回家", textfont));
//		     table.addCell(createCell("吃晚饭", textfont));
//		     totalQuantity++;
//		 }
//	 }
	 {
		 table = createTable(new float[]{180, 360}, 0, 320);
		 table.addCell(createCell("NEW", headfont, Element.ALIGN_CENTER, 0));
		 table.addCell(createCell("EXCEART Guide Sticker Cello Fret Finger", textfont, Element.ALIGN_LEFT, 0));
		 

		 table.addCell(createCell("", textfont, Element.ALIGN_CENTER, 0));
		 table.addCell(createCell("大提琴指板音标贴纸 1个装", textfont, Element.ALIGN_LEFT, 0));
	 }
	 table.setSpacingBefore(12f);
	 
//	 document.add(p1);
	 document.add(image);
//	 document.add(paragraph);
	 document.add(table);
	}
	
	// 生成采购入库单PDF
	public void generateStockIn(Document document) throws Exception {
	
		// 表格
		PdfPTable table1;
		{
			table1 = createTable(new float[]{50, 100, 50}, 0, 320);
			
			table1.addCell(createCell("采购入库单：", textfont10, Element.ALIGN_LEFT, 0));
			table1.addCell(createCell("CGRK-221208-1002", textfont10, Element.ALIGN_LEFT, 0));
			
			// 添加图片
			Image image = Image.getInstance("D://data/goods.jpg");
			image.setAlignment(Image.ALIGN_CENTER);
			image.scalePercent(100); //依照比例缩放
			image.setSpacingBefore(0f);
			table1.addCell(createCellImg(image, 6, 0));
			
			table1.addCell(createCell("采购订单：", textfont10, Element.ALIGN_LEFT, 0));
			table1.addCell(createCell("CGDD-221208-1021-01/02", textfont10, Element.ALIGN_LEFT, 0));
			
			table1.addCell(createCell("公司编码：", textfont10, Element.ALIGN_LEFT, 0));
			table1.addCell(createCell("GS0001", textfont10, Element.ALIGN_LEFT, 0));
			
			table1.addCell(createCell("公司简称：", textfont10, Element.ALIGN_LEFT, 0));
			table1.addCell(createCell("逸思科技", textfont10, Element.ALIGN_LEFT, 0));
			
			table1.addCell(createCell("组装方案：", textfont10, Element.ALIGN_LEFT, 0));
			table1.addCell(createCell("001", textfont10, Element.ALIGN_LEFT, 0));
			
			table1.addCell(createCell("商品信息：", textfont10, Element.ALIGN_LEFT, 0));
			table1.addCell(createCell("PD220630002974/蝴蝶嫁接睫毛隔离片 10件装", textfont10, Element.ALIGN_LEFT, 0));
		}
		
		// 空白表格
		PdfPTable tableBlank = createBlankTable();
	
		// 表格2
		PdfPTable table2;
		{
			table2 = createTable(new float[]{150, 240, 100}, 1, 320);
			
			table2.addCell(createCell("货件清单", textfont14, Element.ALIGN_LEFT, 3, 0));
			
			table2.addCell(createCell("商品编码", textfont12, Element.ALIGN_CENTER, 1));
			table2.addCell(createCell("商品名称", textfont12, Element.ALIGN_CENTER, 1));
			table2.addCell(createCell("采购数量", textfont12, Element.ALIGN_CENTER, 1));
			
			table2.addCell(createCell("PD220630002974-01", textfont10, Element.ALIGN_LEFT, 1));
			table2.addCell(createCell("蝴蝶嫁接睫毛隔离片1个", textfont10, Element.ALIGN_LEFT, 1));
			table2.addCell(createCell("500", textfont10, Element.ALIGN_CENTER, 1));
			
			table2.addCell(createCell("PD220630002974-01", textfont10, Element.ALIGN_LEFT, 1));
			table2.addCell(createCell("蝴蝶嫁接睫毛隔离片1个gae", textfont10, Element.ALIGN_LEFT, 1));
			table2.addCell(createCell("500", textfont10, Element.ALIGN_CENTER, 1));
			
			table2.addCell(createCell("PD220630002974-01", textfont10, Element.ALIGN_LEFT, 1));
			table2.addCell(createCell("蝴蝶嫁接睫毛隔离片1个afe", textfont10, Element.ALIGN_LEFT, 1));
			table2.addCell(createCell("500", textfont10, Element.ALIGN_CENTER, 1));
			
		}
	
		// 表格3
		PdfPTable table3;
		{
			table3 = createTable(new float[]{150, 240, 100}, 1, 320);
			
			table3.addCell(createCell("BOM", textfont14, Element.ALIGN_LEFT, 3, 0));
			
			table3.addCell(createCell("子件编码", textfont12, Element.ALIGN_CENTER, 1));
			table3.addCell(createCell("子件名称", textfont12, Element.ALIGN_CENTER, 1));
			table3.addCell(createCell("子件数量", textfont12, Element.ALIGN_CENTER, 1));
			
			table3.addCell(createCell("PD220630002974-01", textfont10, Element.ALIGN_LEFT, 1));
			table3.addCell(createCell("蝴蝶嫁接睫毛隔离片1个", textfont10, Element.ALIGN_LEFT, 1));
			table3.addCell(createCell("500", textfont10, Element.ALIGN_CENTER, 1));
			
			table3.addCell(createCell("PD220630002974-01", textfont10, Element.ALIGN_LEFT, 1));
			table3.addCell(createCell("蝴蝶嫁接睫毛隔离片1个gae", textfont10, Element.ALIGN_LEFT, 1));
			table3.addCell(createCell("500", textfont10, Element.ALIGN_CENTER, 1));
			
			table3.addCell(createCell("PD220630002974-01", textfont10, Element.ALIGN_LEFT, 1));
			table3.addCell(createCell("蝴蝶嫁接睫毛隔离片1个afe", textfont10, Element.ALIGN_LEFT, 1));
			table3.addCell(createCell("500", textfont10, Element.ALIGN_CENTER, 1));
			
		}
		document.add(table1);
		document.add(tableBlank);
		document.add(table2);
		document.add(tableBlank);
		document.add(table3);
	}
	
	// 生成配货清单PDF
	public void generatePeiHuo(Document document) throws Exception {
	
		// 表格
		PdfPTable table1;
		{
			table1 = createTable(new float[]{50, 100, 50, 100}, 0, 700);
			
			table1.addCell(createCell("配货清单：", textfont14, Element.ALIGN_LEFT, 4, 0));
			table1.addCell(createCell("头程计划单：", textfont12, Element.ALIGN_LEFT, 0));
			table1.addCell(createCell("ZZDD-20220906-0001", textfont10, Element.ALIGN_LEFT, 0));
			table1.addCell(createCell("物流方式：", textfont12, Element.ALIGN_LEFT, 0));
			table1.addCell(createCell("物流编码/物流名称", textfont10, Element.ALIGN_LEFT, 0));
			
			table1.addCell(createCell("公司编码：", textfont12, Element.ALIGN_LEFT, 0));
			table1.addCell(createCell("GS0001", textfont10, Element.ALIGN_LEFT, 0));
			table1.addCell(createCell("公司简称：", textfont12, Element.ALIGN_LEFT, 0));
			table1.addCell(createCell("逸思科技", textfont10, Element.ALIGN_LEFT, 0));
			
			table1.addCell(createCell("预估重量：", textfont12, Element.ALIGN_LEFT, 0));
			table1.addCell(createCell("10kg", textfont10, Element.ALIGN_LEFT, 0));
			table1.addCell(createCell("备注：", textfont12, Element.ALIGN_LEFT, 0));
			table1.addCell(createCell("****", textfont10, Element.ALIGN_LEFT, 0));
		}
		
		// 空白表格
		PdfPTable tableBlank = createBlankTable();
	
		// 表格2
		PdfPTable table2;
		{
			table2 = createTable(new float[]{20, 50, 50, 20, 20, 20, 20, 20, 20}, 1, 700);

			table2.addCell(createCell("明细", textfont14, Element.ALIGN_LEFT, 9, 0));
			
			table2.addCell(createCell("序号", textfont12, Element.ALIGN_CENTER, 1));
			table2.addCell(createCell("商品编码", textfont12, Element.ALIGN_CENTER, 1));
			table2.addCell(createCell("商品名称", textfont12, Element.ALIGN_CENTER, 1));
			table2.addCell(createCell("FNSKU", textfont12, Element.ALIGN_CENTER, 1));
			table2.addCell(createCell("计划发货数量", textfont12, Element.ALIGN_CENTER, 1));
			table2.addCell(createCell("货位", textfont12, Element.ALIGN_CENTER, 1));
			table2.addCell(createCell("库区", textfont12, Element.ALIGN_CENTER, 1));
			table2.addCell(createCell("仓库", textfont12, Element.ALIGN_CENTER, 1));
			table2.addCell(createCell("透明标", textfont12, Element.ALIGN_CENTER, 1));
			
			table2.addCell(createCell("1", textfont10, Element.ALIGN_CENTER, 1));
			table2.addCell(createCell("PD220630002974", textfont10, Element.ALIGN_LEFT, 1));
			table2.addCell(createCell("蝴蝶嫁接睫毛隔离片 10件装", textfont10, Element.ALIGN_CENTER, 1));
			table2.addCell(createCell("X123456", textfont10, Element.ALIGN_CENTER, 1));
			table2.addCell(createCell("100", textfont10, Element.ALIGN_CENTER, 1));
			table2.addCell(createCell("C100", textfont10, Element.ALIGN_CENTER, 1));
			table2.addCell(createCell("A", textfont10, Element.ALIGN_CENTER, 1));
			table2.addCell(createCell("福州仓", textfont10, Element.ALIGN_CENTER, 1));
			table2.addCell(createCell("文件名", textfont10, Element.ALIGN_CENTER, 1));
			
			table2.addCell(createCell("1", textfont10, Element.ALIGN_CENTER, 1));
			table2.addCell(createCell("PD220630002974", textfont10, Element.ALIGN_LEFT, 1));
			table2.addCell(createCell("蝴蝶嫁接睫毛隔离片 10件装", textfont10, Element.ALIGN_CENTER, 1));
			table2.addCell(createCell("X123456", textfont10, Element.ALIGN_CENTER, 1));
			table2.addCell(createCell("100", textfont10, Element.ALIGN_CENTER, 1));
			table2.addCell(createCell("C100", textfont10, Element.ALIGN_CENTER, 1));
			table2.addCell(createCell("A", textfont10, Element.ALIGN_CENTER, 1));
			table2.addCell(createCell("福州仓", textfont10, Element.ALIGN_CENTER, 1));
			table2.addCell(createCell("文件名", textfont10, Element.ALIGN_CENTER, 1));
			
		}
		document.add(table1);
		document.add(tableBlank);
		document.add(table2);
	}
	
	/**
	 * 创建指定列宽、列数的表格
	 */
	public PdfPTable createTable(float[] widths, Integer border, Integer totalWidth) {
	    PdfPTable table = new PdfPTable(widths);
	    try {
	        table.setTotalWidth(totalWidth);
	        table.setLockedWidth(true);
	        table.setHorizontalAlignment(Element.ALIGN_CENTER);
	        table.getDefaultCell().setBorder(border);
	    } catch (Exception e) {
	        e.printStackTrace();
	    }
	    return table;
	}

	/**
	 * 创建空白的表格
	 */
	public PdfPTable createBlankTable() {
	    PdfPTable table = new PdfPTable(1);
	    table.getDefaultCell().setBorder(0);
	    table.addCell(createCell("", keyfont, 0));
	    table.setSpacingAfter(20.0f);
	    table.setSpacingBefore(20.0f);
	    return table;
	}

	/**
	 * 创建单元格(指定字体)
	 */
	public PdfPCell createCell(String value, Font font, Integer border) {
		PdfPCell cell = new PdfPCell();
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cell.setHorizontalAlignment(Element.ALIGN_CENTER);
		cell.setPhrase(new Phrase(value, font));
		cell.setBorder(border);
		return cell;
	}
	
	/**
	 * 创建单元格（指定字体、水平..）
	 */
	public PdfPCell createCell(String value, Font font, int align, Integer border) {
	    PdfPCell cell = new PdfPCell();
	    cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
	    cell.setHorizontalAlignment(align);
	    cell.setPhrase(new Phrase(value, font));
		cell.setBorder(border);
	    return cell;
	}

	/**
	 * 创建单元格（指定字体、水平居..、单元格跨x列合并）
	 *
	 * @param value
	 * @param font
	 * @param align
	 * @param colspan
	 * @return
	 */
	public PdfPCell createCell(String value, Font font, int align, int colspan, Integer border) {
	    PdfPCell cell = new PdfPCell();
	    cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
	    cell.setHorizontalAlignment(align);
	    cell.setColspan(colspan);
	    cell.setPhrase(new Phrase(value, font));
		cell.setBorder(border);
	    return cell;
	}

	/**
	 * 创建单元格（指定字体、水平居..、单元格跨x列合并、设置单元格内边距）
	 *
	 * @param value
	 * @param font
	 * @param align
	 * @param colspan
	 * @param boderFlag
	 * @return
	 */
	public PdfPCell createCell(String value, Font font, int align, int colspan, boolean boderFlag, Integer border) {
	    PdfPCell cell = new PdfPCell();
	    cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
	    cell.setHorizontalAlignment(align);
	    cell.setColspan(colspan);
	    cell.setPhrase(new Phrase(value, font));
	    cell.setPadding(3.0f);
	    if (!boderFlag) {
	        cell.setPaddingTop(15.0f);
	        cell.setPaddingBottom(8.0f);
	    } else if (boderFlag) {
	        cell.setPaddingTop(0.0f);
	        cell.setPaddingBottom(15.0f);
	    }
		cell.setBorder(border);
	    return cell;
	}

	/**
	 * 创建单元格（指定字体、水平..、边框宽度：0表示无边框、内边距）
	 *
	 * @param value
	 * @param font
	 * @param align
	 * @param borderWidth
	 * @param paddingSize
	 * @param flag
	 * @return
	 */
	public PdfPCell createCell(String value, Font font, int align, float[] borderWidth, float[] paddingSize, boolean flag, Integer border) {
	    PdfPCell cell = new PdfPCell();
	    cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
	    cell.setHorizontalAlignment(align);
	    cell.setPhrase(new Phrase(value, font));
	    cell.setBorderWidthLeft(borderWidth[0]);
	    cell.setBorderWidthRight(borderWidth[1]);
	    cell.setBorderWidthTop(borderWidth[2]);
	    cell.setBorderWidthBottom(borderWidth[3]);
	    cell.setPaddingTop(paddingSize[0]);
	    cell.setPaddingBottom(paddingSize[1]);
	    if (flag) {
	        cell.setColspan(2);
	    }
		cell.setBorder(border);
	    return cell;
	}
	
	/*
	 * 创建单元格(指定字体)
	 */
	public PdfPCell createCellImg(Image image, Integer border) {
		PdfPCell cell = new PdfPCell();
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cell.setHorizontalAlignment(Element.ALIGN_CENTER);
		cell.setImage(image);
		cell.setBorder(border);
		return cell;
	}
	
	/*
	 * 创建单元格(图片，跨列)
	 */
	public PdfPCell createCellImg(Image image, Integer rowspan, Integer border) {
		PdfPCell cell = new PdfPCell();
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cell.setHorizontalAlignment(Element.ALIGN_CENTER);
		cell.setImage(image);
		cell.setBorder(border);
		if (rowspan != null && rowspan > 1) {
			cell.setRowspan(rowspan);
		}
		return cell;
	}

	/**
	 * 创建单元格(指定字体、水平对齐、合并行单元格)
	 */
	public PdfPCell createCellText(String value, Font font, Integer horizontalAlignment, Integer colspan, Integer border) {
		PdfPCell cell = new PdfPCell();
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cell.setHorizontalAlignment(horizontalAlignment);
		cell.setPhrase(new Phrase(value, font));
		cell.setBorder(border);
		cell.setColspan(colspan);
		return cell;
	}


}
